#!/opt/php/bin/php -d display_errors=1
<?php
require_once 'TVB/Message/BaseConsumer.php';
require_once 'TVB/Message/MessageHelper.php';

class Consumer extends BaseConsumer
{
    protected function process(Stomp_Frame $message)
    {
		$vi_data = array(
			'duration'=>222,
			'breakpoint'=>'2,33,4',
			'fms' => array(
				'smil' => 'http://fms.api.tvb.com/getSmil.php?source_id=3840',
				'452000' => array(  // export_id
					'exportId' => 3840,
					'width' => 720,
					'height' => 406,
					'videoCodec' => 'AVC',
					'videoBitrate' => 450000,
					'frameRate' => 25,
					'pixelAspectRatio' => 1,
					'audioCodec' => 'AAC',
					'audioBitrate' => 48000,
					'audioChannel' => 2,
					'audioSampleRate' => 44100,
					'exportArgs' =>'',
					'formatId' => 1,
					'streamConnectionUrl' => 'rtmpe://streaming.tvb.com/vi',
					'streamFilePath' => 'mp4:/5/3/000000000254.mp4',
					'streamUrl' => 'rtmpe://streaming.tvb.com/vi//5/3/000000000254.mp4'
				),
				'176000' => array(
					'exportId' => 3840,
					'width' => 320,
					'height' => 180,
					'videoCodec' => 'AVC',
					'videoBitrate' => 152000,
					'frameRate' => 25,
					'pixelAspectRatio' => 1,
					'audioCodec' => 'AAC',
					'audioBitrate' => 48000,
					'audioChannel' => 2,
					'audioSampleRate' => 44100,
					'exportArgs' => '',
					'formatId' => 1,
					'streamConnectionUrl' => 'rtmpe://streaming.tvb.com/vi',
					'streamFilePath' => 'mp4:/5/3/000000000255.mp4',
					'streamUrl' => 'rtmpe://streaming.tvb.com/vi//5/3/000000000255.mp4',
				)
			)
		);
		$ii_data = array(  // INTxINT 或者 origin 等下標都是根據 folder_type 來決定的
			'ii_id' => 1141,
			'125x70' => array(
				'url' => 'http://img.tvb.com/ii/1/6/000000000508_1276505203.jpg',
				'watermark' => 0,
				'type' => 'synopsis_image'
			),
			'512x288' => array(
				'url' => 'http://img.tvb.com/ii/1/6/000000000507_1276505201.jpg',
				'watermark' => 0,
				'type' => 'synopsis_image'
			),
			'100x75' => array(
				'url' => 'http://img.tvb.com/ii/1/6/000000000509_1276505204.jpg',
				'watermark' => 0,
				'type' => 'synopsis_image'
			),
			'70x70' => array(
				'url' => 'http://img.tvb.com/ii/1/6/000000000510_1276505205.jpg',
				'watermark' => 0,
				'type' => 'synopsis_image'
			),
			'136x76' => array(
				'url' => 'http://img.tvb.com/ii/1/6/000000000520_1276591533.jpg',
				'watermark' => 0,
				'type' => 'synopsis_image'
			),
			'373x210' => array(
				'url' => 'http://img.tvb.com/ii/1/6/000000000521_1276591534.jpg',
				'watermark' => 0,
				'type' => 'synopsis_image'
			),
			'100x100' => array(
				'url' => 'http://img.tvb.com/ii/1/6/000000000512_1276505207.jpg',
				'watermark' => 0,
				'type' => 'synopsis_image'
			),
			'640x480' => array(
				'url' => 'http://img.tvb.com/ii/1/6/000000000513_1276505208.jpg',
				'watermark' => 0,
				'type' => 'thumbnail'
			),
			'900x900' => array(
				'url' => 'http://img.tvb.com/ii/1/6/000000000514_1276505209.jpg',
				'watermark' => 0,
				'type' => 'thumbnail'
			),
			'origin' => array(
				'url' => 'http://img.tvb.com/ii/1/6/000000000514_1276505209.jpg',
				'watermark' => 0,
				'type' => 'full_photo'
			),
		);

		$data = (array)$message->map;

		#print_r($data);

		$vi_ids = $data['vi_ids'];
		$ii_ids = $data['ii_ids'];
		$data['vi_ids'] = $data['ii_ids'] = array();

		# parse vi data
		foreach($vi_ids as $vi_id){ 
			$data['vi_ids'][$vi_id] = $vi_data; 
		}

		# parse ii data
		foreach($ii_ids as $ii_id => $folder_type){
			if(empty($folder_type)){ // is video/chapter thumbnail 
				// ??
			}
			$data['ii_ids'][$ii_id] = $ii_data;
		}

		$td = json_decode(bzdecompress(base64_decode($data['callback_data']->db3)),1);
		#echo "\n===============================\nAction: {$data['action']} \n";
		foreach($td as $row){
			$table = strtolower($row['type']);
			if('p_' == substr((string)$data['action'],0,2)){
				$table = 'p_'.$table;
			}
			#echo "Table: {$table} DataKey:",print_r(array_keys($row['data']),1);
			#$fields = array();
			#exec("mysql -uroot -hvm043.dev.hk3.tvb.com -e 'desc pdes_02.$table' | awk '{print $1}'",$fields);
			#array_shift($fields);
			#echo "DbDey:",print_r($fields,1),"Diff:";
			#print_r(array_diff($fields,array_keys($row['data'])));
		}


		$config = simplexml_load_file('/data/mytvplus/portal_service/conf/consumer.xml');
		$consumers_info = $config->xpath("/config/consumer[@name='PublishDb3AndPortal']");
		$config = $consumers_info[0];
		$broker = (string)$config->broker;
		$queue  = (string)$config->queue;
		print_r($data);
		return MessageHelper::sendMessage($broker,$queue,$data);
	}
}


$cons = new Consumer('/data/pms/pdes/conf/consumer.xml', 'videoImageHandler');
$cons->run();
?>
